package com.kucan.recyclecard.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kucan.recyclecard.R;
import com.kucan.recyclecard.adapters.MyViewPagerAdapter;
import com.kucan.recyclecard.base.BaseActivity;
import com.kucan.recyclecard.core.ActionCallbackListener;
import com.kucan.recyclecard.view.CustomerDialog;
import com.kucan.recyclecard.view.NoScrollViewPager;

public class MainActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private NoScrollViewPager vp_main;
    private ImageView iv_customer;
    public MyViewPagerAdapter mPagerAdapter;
    private CustomerDialog customerDialog;
    private RadioGroup rg_control_button;
    private RadioButton rb_home, rb_market, rb_order, rb_invita, rb_mine;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setViewPager();
        initData();
    }

    private void initData() {
        mAppAction.getConfig(new ActionCallbackListener<String>() {
            @Override
            public void onSuccess(String data) {
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    private void setViewPager() {
        if (mPagerAdapter == null) {
            mPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
            vp_main.setAdapter(mPagerAdapter);
            vp_main.setOffscreenPageLimit(mPagerAdapter.getCount());
            vp_main.setNoScroll(true);
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {
        vp_main = $(R.id.vp_main);
        iv_customer = $(R.id.iv_customer);
        rb_home = $(R.id.rb_home);
        rb_market = $(R.id.rb_market);
        rb_order = $(R.id.rb_order);
        rb_invita = $(R.id.rb_invita);
        rb_mine = $(R.id.rb_mine);
        rg_control_button = $(R.id.rg_control_button);
    }

    @Override
    protected void setListeners() {
        iv_customer.setOnClickListener(this);
        rg_control_button.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_customer) {
            if (customerDialog == null) {
                customerDialog = new CustomerDialog(this);
            }
            customerDialog.show();
            return;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_home) {
            vp_main.setCurrentItem(0, false);
            return;
        }

        if (checkedId == R.id.rb_invita) {
            vp_main.setCurrentItem(1, false);
            return;
        }

        if (checkedId == R.id.rb_market) {
            vp_main.setCurrentItem(2, false);
            return;
        }

        if (checkedId == R.id.rb_order) {
            vp_main.setCurrentItem(3, false);
            return;
        }

        if (checkedId == R.id.rb_mine) {
            vp_main.setCurrentItem(4, false);
        }
    }

    /**
     * 模拟点击跳转tab
     *
     * @param index
     */
    public void turnToPage(int index) {
        switch (index) {
            case 0:
                rb_home.performClick();
                break;
            case 1:
                rb_invita.performClick();
                break;
            case 2:
                rb_market.performClick();
                break;
            case 3:
                rb_order.performClick();
                break;
            case 4:
                rb_mine.performClick();
                break;
        }
    }
}